<template>
  <div id="register" class="background">
    <div class="login-box login-box-radius">
      <h1>登录</h1>
      <el-form ref="userForm" :model="userForm" :rules="rules">
        <el-form-item prop="account">
          <el-input placeholder="用户名" v-model="userForm.account"></el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input placeholder="密码" type="password" v-model="userForm.password"></el-input>
        </el-form-item>

        <el-form-item size="small" class="login-button">
          <el-button type="primary" @click.native.prevent="login('userForm')">登录</el-button>
        </el-form-item>
      </el-form>
      <el-button type="text" @click="jumpReg()">注册账户</el-button>
      <el-button style="margin-left: 60%"
          type="text" @click="jumpInd()">返回主页</el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: "login",
  data() {
    return {
      userForm: {
        account: '',
        password: ''
      },
      rules: {
        account: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {max: 10, message: '不能大于10个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {max: 10, message: '不能大于10个字符', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    jumpReg() {
      this.$router.push('/register')
    },
    jumpInd() {
      this.$router.push('/')
    },
    login(formName) {
      //提交前验证填写规则是否符合
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //存储后台返回的token令牌
          this.$store.dispatch('login', this.userForm).then(() => {
            this.$message({message: '登录成功', type: 'success', showClose: true});
            if(this.userForm.account === "admin"){
              this.$router.push({path: '/admin'})
            } else {
              this.$router.push({path: '/'})
            }
          }).catch((error) => {
            if (error !== 'error') {
              this.$message({message: error, type: 'error', showClose: true});
            }
          })
        } else {
          return false;
        }
      });

    }

  }
}
</script>

<style scoped>
.background {
  background: url("../assets/bk.png") no-repeat;
  background-position: center;
  height: 100%;
  width: 100%;
  background-size: cover;
  position: fixed;
}

#login {
  min-width: 100%;
  min-height: 100%;
}

.video-player {
  background-color: transparent;
  width: 100%;
  height: 100%;
  object-fit: fill;
  display: block;
  position: absolute;
  left: 0;
  z-index: 0;
  top: 0;
}

.login-box {
  position: absolute;
  width: 300px;
  height: 260px;
  background-color: white;
  margin-top: 150px;
  margin-left: -180px;
  left: 50%;
  padding: 30px;
}

.login-box-radius {
  border-radius: 10px;
  box-shadow: 0px 0px 1px 1px rgba(161, 159, 159, 0.1);
}

.login-box h1 {
  text-align: center;
  font-size: 24px;
  margin-bottom: 20px;
  vertical-align: middle;
}

.login-design {
  text-align: center;
  font-family: 'Open Sans', sans-serif;
  font-size: 18px;
}

.login-design-color {
  color: #8690A6 !important;
}

.login-button {
  text-align: center;
}

.login-button button {
  width: 100%;
}

</style>
